package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.order.base.consts.KeyValues;

/**
 * APN类型属性处理器
 */
public class ApnTypeAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private OperateInstAttrService operateInstAttrService;
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void onShow(KiteAttr attr, KiteInst inst, AttrData data) {
        // 隐藏“速率”和“使用PCRF”
        String apnType = inst.getAttrValue("200003999");
        if (!KiteStringUtils.equals(apnType, "100006957")) { // 选择互联网
            KiteAttr speedAttr = inst.getAttr("200004001"); //细分业务类型
            if (!KiteObjectUtils.isEmpty(speedAttr)) {
                speedAttr.getAttrValues()
                    .removeIf(attrValue -> KiteStringUtils.equals("100006960", attrValue.getAttrValue())); //剔除互联网GRE
                speedAttr.getAttrValues()
                    .removeIf(attrValue -> KiteStringUtils.equals("100006961", attrValue.getAttrValue())); //剔除互联网L2TP
                speedAttr.getAttrValues()
                    .removeIf(attrValue -> KiteStringUtils.equals("100006962", attrValue.getAttrValue())); //剔除专线GRE
                speedAttr.getAttrValues()
                    .removeIf(attrValue -> KiteStringUtils.equals("100006963", attrValue.getAttrValue())); //剔除专线L2TP
            }
        }
        else { // 选择VPDN
            KiteAttr speedAttr = inst.getAttr("200004001"); //细分业务类型
            if (!KiteObjectUtils.isEmpty(speedAttr)) {
                speedAttr.getAttrValues()
                    .removeIf(attrValue -> KiteStringUtils.equals("100006958", attrValue.getAttrValue())); //剔除白名单
                speedAttr.getAttrValues()
                    .removeIf(attrValue -> KiteStringUtils.equals("100006959", attrValue.getAttrValue())); //剔除黑名单
            }
            // 操作类型
            KiteAttr operTypeAttr = inst.getAttr("200002753");
            if (Objects.nonNull(operTypeAttr)) {
                // 选择变更互联网地址时 客户公网IP可空
                if (KiteStringUtils.equals("100007395", operTypeAttr.getValue())) {
                    Optional.ofNullable(inst.getAttr("200004007"))
                        .ifPresent(kiteAttr -> kiteAttr.setIsNull(KeyConsts.IFTRUE_T));
                }
            }
        }
    }

    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.set(attr, inst, value, data);
        //模版还原时，重提还原
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if ("sceneTempl".equals(data.getExtAttributes().get("entry")) || (KiteMapUtils.isNotEmpty(extParams) && extParams.containsKey("nodeCode"))) {
            return;
        }
        //变更业务的场景初始化过程
        if (!KiteStringUtils.equalsAny(data.getServiceOfferId(), LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK, ServiceOfferConsts.SERVICE_OFFER_ORDER)
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), "initSource"))) {
            // 变更业务 APN类型不可编辑
            if (KiteStringUtils.equalsAny(value, "100003262", "100006957") && attr.getLoadFlag()) {
                attr.setIsEdit(KeyConsts.IFTRUE_F);
            }
            return;
        }
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(data.getSceneInstId());
        kiteProdInstList.stream().forEach(kiteProdInst -> {
            String prodId = kiteProdInst.getProdId();
            Optional.ofNullable(inst.getAttr("200004001"))
                .ifPresent(funcAttr -> {
                    if ((!KiteStringUtils.equals(prodId, ProdCateUtil.getMainMemberProdId(ProdCateUtil.DZNB_PROD))
                        || !KiteStringUtils.equals("100003262", value)) && Objects.nonNull(inst.getAttr("200003996"))
                        && KiteStringUtils.equals(inst.getAttr("200003996").getValue(), "100006952")) {
                        funcAttr.setIsNull(KeyConsts.IFTRUE_F);
                        funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    }
                });
        });
        List<KiteAttr> attrs = inst.listAttrs();
        attrs.forEach(kiteAttr->{
            if(!KiteStringUtils.isEqual(kiteAttr.getFieldName(),attr.getFieldName()) && !KiteStringUtils.isEqual(kiteAttr.getFieldName(),"200003996")
                && (!KiteStringUtils.isEqual(KeyConsts.PROD_ATTR_SHOW_TYPE_E,kiteAttr.getShowType()) || KiteStringUtils.isEqual(kiteAttr.getFieldName(),"200004001"))){
                List<KiteAttrValue> attrValues =  kiteAttr.getAttrValues();
                String attrType = kiteAttr.getAttrValueTypeId();
                if(KiteListUtils.isNotEmpty(attrValues) && !KiteStringUtils.equals(attrType, KeyValues.INPUT)){
                    String defaultValue = kiteAttr.getDefaultValue();
                    List<KiteAttrValue> filterAttrValues = attrValues.stream().filter(AttrValue -> KiteStringUtils.isEqual(AttrValue.getAttrValue(),defaultValue)).collect(Collectors.toList());
                    KiteAttrValue attrValue = attrValues.get(0);
                    if(KiteStringUtils.isEmpty(defaultValue) || KiteListUtils.isEmpty(filterAttrValues)){
                        operateInstAttrService.setAttrValue(inst, kiteAttr.getFieldName(), attrValue.getAttrValue(), data.getSceneInstId(), null);
                    }else{
                        operateInstAttrService.setAttrValue(inst, kiteAttr.getFieldName(), kiteAttr.getDefaultValue(), data.getSceneInstId(), null);
                    }
                }else{
                    operateInstAttrService.setAttrValue(inst, kiteAttr.getFieldName(), kiteAttr.getDefaultValue(), data.getSceneInstId(), null);
                }
            }
        });

        kiteProdInstList.stream().forEach(kiteProdInst -> {
            String prodId = kiteProdInst.getProdId();
            Optional.ofNullable(inst.getAttr("200004001"))
                .ifPresent(funcAttr -> {
                    if (KiteStringUtils.equals(prodId, ProdCateUtil.getMainMemberProdId(ProdCateUtil.DZNB_PROD)) && KiteStringUtils.equals("100003262",value)) {
                        funcAttr.setIsNull(KeyConsts.IFTRUE_T);
                        funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    }
                });
        });
    }
}
